package com.shen.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shen.commonutils.JwtUtils;
import com.shen.commonutils.MD5;
import com.shen.educenter.entity.UcenterMember;
import com.shen.educenter.entity.vo.RegisterVo;
import com.shen.educenter.mapper.UcenterMemberMapper;
import com.shen.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shen.exceptionHandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author shen
 * @since 2020-07-03
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public String login(UcenterMember member) {
        //获取登录的密码和手机号
        String mobile = member.getMobile();
        String password = member.getPassword();
        if(StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)){

            throw  new GuliException(20001,"登录失败");
        }
        //判断手机号是否正确 或者 this
        QueryWrapper<UcenterMember> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        UcenterMember mobileMember = baseMapper.selectOne(queryWrapper);
        if(mobileMember==null){
            throw  new GuliException(20001,"登录失败");
        }
        // 判断密码 存储到数据库的密码进行了加密
        if(!MD5.encrypt(password).equals(mobileMember.getPassword())){
            throw  new GuliException(20001,"登录失败,密码错误");
        }
        //判断是否被禁用
        if(mobileMember.getIsDisabled()){
            throw  new GuliException(20001,"登录失败,账号被禁用");
        }
        //登录成功返回token--使用jwt生成--（三部分 头 data 签名hash 组成）
        String token = JwtUtils.getJwtToken(mobileMember.getId(),mobileMember.getNickname());

        return token;
    }

    @Override
    public void register(RegisterVo registerVo) {
        String nickname = registerVo.getNickname();
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();
        if(StringUtils.isEmpty(mobile) ||

        StringUtils.isEmpty(mobile) ||

        StringUtils.isEmpty(password) ||

        StringUtils.isEmpty(code)) {

            throw new GuliException(20001,"error");
        }
        //先获取redis数据
        String mobleCode = redisTemplate.opsForValue().get(mobile);
        if(!code.equals(mobleCode)) {
            throw new GuliException(20001,"error");
        }
        //查询数据库中是否存在相同的手机号码
        Integer count = baseMapper.selectCount(new QueryWrapper<UcenterMember>().eq("mobile", mobile));
        if(count.intValue() > 0) {
            throw new GuliException(20001,"error");
        }
        //添加注册信息到数据库
        UcenterMember member = new UcenterMember();
        member.setNickname(nickname);
        member.setMobile(registerVo.getMobile());
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        member.setAvatar("https://shenkaituo.oss-cn-shenzhen.aliyuncs.com/2020/06/25/805357e494cf416bb0e54e9a673174c3qin2.jpg");
        this.save(member);
        //  baseMapper.insert(member);



    }

    @Override
    public UcenterMember getByOpenId(String openid) {
        QueryWrapper<UcenterMember> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(queryWrapper);
        return member;
    }

    @Override
    public Integer countRegister(String day) {
        return baseMapper.countRegister(day);
    }
}
